/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "gpsposition.h"
#include "gpsfix.h"

#include <QtCore/QObject>
#include <QtCore/QTime>
#include <QtCore/QString>

#include "gpsdll.h"

namespace greenhills {
namespace gps {

/** @brief Encapsulation of GPS receiver
 *
 * This class encapsulates GPS receiver and provides necessary GPS position
 * information and also further information obtained from GPS like
 * movementSpeed and movement direction.
 * In order to work properly, a CGPS object has to be connected to a NMEA parser
 * object using appropriate slots.
 */

class CNMEAParser;

class GH_GPS_QT_CLASS CGPS : public QObject
{
Q_OBJECT;

public:
    /**
     * @brief Initializes the instance of CGPS object
     */
    CGPS(gps::CNMEAParser *pParser, QObject *pParent=0);

    /**
     * @brief Destroys the instance of CGPS object
     */
    ~CGPS();

public:
    /**
     * @brief Provides information about current time obtained from GPS.
     *
     * @return Returns current time in QTime format.
     */
    QTime time() const {return mTime;}

    /**
     * @brief Provides information about current GPS position.
     *
     * @return Returns current GPS position in CGPSPosition format.
     */
    CGPSPosition position() const {return mPosition;}

    /**
     * @brief Provides information about GPS fix.
     *
     * @return Returns GPS fix in CGPSFix format.
     */
    CGPSFix fix() const {return mFix;}

    /**
     * @brief Provides information about satellites in use.
     *
     * @return Returns number of satellites in use.
     */
    int satelites() const {return mSatelites;}

    /**
     * @brief Provides information about atitude.
     *
     * @return Returns atitude in meters above sea level.
     */
    float altitude() const {return mAltitude;}

    /**
     * @brief Provides HDOP value (Horizontal Dilution of Precision).
     *
     * @return Returns HDOP value which express relative accuracy of horizontal
     * position.
     */
    float hdop() const {return mHdop;}

    /**
     * @brief Provides value of movement speed .
     *
     * @return Returns movement speed value in kilometers per hour.
     */
    float movementSpeed() const {return mMovementSpeed;}

    /**
     * @brief Provides value of movement direction.
     *
     * @return Returns movement direction value in degrees.
     */
    float movementDirection() const {return mMovementDirection;}

public Q_SLOTS:
    /**
     * @brief Receives GPGGA sentence parameters from NMEA parser
     */
    void onGPGGAreceived(const QTime &pTime,
        const greenhills::gps::CGPSPosition &pPosition,
        const greenhills::gps::CGPSFix &pFix,
        int pSatelites, float pHdop, float pAltitude, float pGeoidHeight,
        float pDgpsUpdateDelay, int pDgpsStationId);

    /**
     * @brief Receives GPVTG sentence parameters from NMEA parser
     */
    void onGPVTGreceived(float pHeadingTrueNorth, float pHeadingMagnetic,
        float pSpeedInKnots, float pSpeedInKilometers);

Q_SIGNALS:

    /**
     * @brief This signal is emitted when gps time is changed.
     */
    void timeChanged(const QTime &pVal);
    /**
     * @brief This signal is emitted when position is changed.
     */
    void positionChanged(const greenhills::gps::CGPSPosition &pVal);
    /**
     * @brief This signal is emitted when gps fix value is changed.
     */
    void fixChanged(const greenhills::gps::CGPSFix &pVal);
    /**
     * @brief This signal is emitted when satelites value is changed.
     */
    void satelitesChanged(int pVal);
    /**
     * @brief This signal is emitted when altitude value is changed.
     */
    void altitudeChanged(float pVal);
    /**
     * @brief This signal is emitted when HDOP value is changed.
     */
    void hdopChanged(float pVal);
    /**
     * @brief This signal is emitted when movement speed value is changed.
     */
    void movementSpeedChanged(int pVal);
    /**
     * @brief This signal is emitted when movement direction value is changed.
     */
    void movementDirectionChanged(int pVal);

    /**
     * @brief This signal is emitted when any of gps values is changed.
     */
    void statusChanged();

private:
    QTime mTime;
    CGPSPosition mPosition;
    CGPSFix mFix;
    int mSatelites;
    float mAltitude;
    float mHdop;
    float mMovementSpeed;
    float mMovementDirection;
};

} //namespace gps
} //namespace greenhills
